import React, {Component} from 'react';
import ButtonConfirmationBox from './ChildrenComponent/ButtonConfirmationBox';
import TimeShow from './ChildrenComponent/TimeShow';
import BiaoZhunForm from './ChildrenComponent/BiaoZhunForm';
import 'antd/dist/antd.css';
import './BiaoZhun.css'
import moment from 'moment';


class BiaoZhun extends Component {
    constructor(props) {
        super(props);
        this.state = {
            riqi: moment().format('YYYY-MM-DD'),
            formData: [[[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []]],//原始填写的数据
            zhibiao: [[], [], [], []],//从数据库获取的标准
            person: 'ROOT',//传入的登录人员
            tableChose: 0,//选择的表格
        }
    }

    //判定是否已登录，是否有权限
    componentWillMount() {
        fetch('/api/JudgePrivilege', {
            method: 'post',
            credentials: 'include',
            headers: {
                'Content-Type': 'application/json',
                'authorization': window.localStorage.authorization,
            }
        })
            .then(res => res.json())
            .then(data => {
                if (data['code'] === 0) {
                }
                else{
                    this.props.history.push('/index');
                }
            })
            .catch(error => console.error('Error:', error))
    }

    componentDidMount() {
        const leixing_list = ['RYS', 'CMS', "KZS", "SSY"];
        let a = this.state.zhibiao;
        for (let j = 0; j < leixing_list.length; j++) {
            const leixing = {'leixing': leixing_list[j]};
            fetch("/api/zhibiao/query", {
                method: 'POST',
                body: JSON.stringify(leixing), // data can be `string` or {object}!
                headers: {
                    'Content-Type': 'application/json',
                    'authorization': window.localStorage.authorization,
                }
            })
                .then(res => res.json())
                .then(data => {
                        if (data['code'] === 0 && data['zhibiao']['leixing'] === leixing['leixing']) { //判定是否成功
                            let zhi = data['zhibiao']['zhibiao'];//去除指标的逗号
                            const zhibiao = zhi.split(',');
                            for (let i = 0; i < zhibiao.length; i++)
                                a[j][i] = parseFloat(zhibiao[i]);
                            this.setState({
                                zhibiao: a,
                            });
                        }
                    }
                )
                .catch(error => console.error('Error:', error))
        }

    }

    /**表格选择**/
    handleTableChose(x) {
        this.setState({
            tableChose: x//x代表选择的表格
        });
    }

    /**onRef控制子组件提交表单**/
    onRef = (ref) => {
        this.BottomForm = ref;
    };
    /**点击提交数据**/
    handleSubmit = () => {
        this.BottomForm.postToHome();
    };

    /***返回按钮事件 ***/
    returnBack = () => {
        this.props.history.push("/index");
    };

    render() {
        return (
            <div style={{margin: '30px 30px 30px 30px'}}>
                <h1 align="center">设置标准值</h1>
                {/*表单最上的时间及人员显示*/}
                <TimeShow person={this.state.person} handleTableChose={this.handleTableChose.bind(this)}/>
                <div style={{
                    border: '2px solid black',
                    margin: '0px 20px 0px 20px'
                }}>
                    {/*表单数据部分*/}
                    <BiaoZhunForm onRef={this.onRef}
                                  authorization={this.state.authorization}
                                  standard={this.state.zhibiao[this.state.tableChose]}
                                  tableChose={this.state.tableChose}
                                  person={this.state.person}
                                  formData={this.state.formData[this.state.tableChose]}
                                  riqi={this.state.riqi}
                    />
                </div>
                {/* <div
                    style={{
                        margin: "0px 0px 20px 50px",
                        display: "inline-block"
                    }}
                >
                    <ButtonConfirmationBox action={this.returnBack} buttonText="返回"/>
                </div> */}
                <div
                    style={{
                        float: "right",
                        margin: "0px 50px 20px 0px",
                        display: "inline-block"
                    }}
                >
                    <ButtonConfirmationBox type="primary" buttonText="提交" action={this.handleSubmit}/>
                </div>
            </div>
        );
    }
}

export default BiaoZhun;